package com.jxc.action.xsgl;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.po.BasicArgument;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.VSaleReturnlist;
import com.jxc.model.vo.VSalelist;
import com.jxc.model.vo.VSalelistItem;
import com.jxc.service.BasicArgumentServiceI;
import com.jxc.service.SaleReturnlistServiceI;
import com.jxc.service.SalelistServiceI;
import com.jxc.util.Constants;
import com.jxc.util.ExceptionUtil;
import com.opensymphony.xwork2.ModelDriven;
@Namespace("/xsgl")
@Action(value = "saleReturnlistAction", results = {})
public class SaleReturnlistAction extends BaseAction implements
		ModelDriven<VSaleReturnlist> {
	private final static Logger logger = Logger.getLogger(SaleReturnlistAction.class);

	@Autowired
	private SalelistServiceI salelistService;
	@Autowired
	private BasicArgumentServiceI basicArgumentService;
	@Autowired
	private SaleReturnlistServiceI saleReturnlistService;

	private VSaleReturnlist model = new VSaleReturnlist();

	private String items;
	
	@Override
	public VSaleReturnlist getModel() {
		return model;
	}

	public void noAuth_statusReturn() {
		String sid = getRequest().getParameter("sid");
		String ids = getRequest().getParameter("ids");
		VSalelist vSalelist = salelistService.getSalelistById(Long
				.parseLong(sid));
		BasicArgument argument = basicArgumentService.getBasricArgumentByKey(vSalelist.getRid(), Constants.INTEGRAL_CONVERSION_RATIO);
		List<VSalelistItem> vSalelistItems = salelistService.getItemsByIds(ids);
		VSaleReturnlist vSaleReturnlist = new VSaleReturnlist();
		Double amount = 0d;
		for (VSalelistItem item : vSalelistItems) {
			amount += item.getTotal();
		}
		if(vSalelist.getPointNum()!=null&&vSalelist.getPointNum()>0){
			vSaleReturnlist.setReturnPoint(vSalelist.getPointNum());
			amount -= Double.valueOf(vSalelist.getPointNum())/Integer.parseInt(argument.getArgValue());
		}else{
			vSaleReturnlist.setReturnPoint(0);
		}
		vSaleReturnlist.setRealReturn(amount);
		writeJson(vSaleReturnlist);
	}
	
	public void noAuth_add(){
		Json j = new Json();
		try {
			saleReturnlistService.add(model, items);
			j.setMsg("退货成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("退货失败！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}

}
